<?php 
define("CCTRL", "user");
define("CCTRLNAME", "user");
define("TOKEN", "bb6222905ef8419183b5437779497596");
class user extends BACKEND_Controller  {

        /* 
            Project     : 48c6c450f1a4a0cc53d9585dc0fee742
            Created on  : Mar 16, 2013, 11:29:15 PM
            Author      : Truong Khuong - khuongxuantruong@gmail.com
            Description :
                Purpose of the stylesheet follows.
        */
        function __construct(){
            parent::__construct();
            $this->load->model("backend/user_model");
            $this->smarty=new CI_Smarty3();
            $this->smarty->error_reporting = E_ALL & ~E_NOTICE;
            if(!isset($_SESSION["ADP"][CCTRL]["display"]))$_SESSION["ADP"][CCTRL]["display"]=0;
        }
        function index(){
            //var_dump( $this->auth_backend->privilege );
            $this->smarty->display("backend/admin/01_auth_user");
        }
        function gridscript(){
            unset($_SESSION["ADP"][CCTRL."-JQX"]);
            if(isset($_SESSION["ADP"][CCTRL."-JQX"]["colModel"]))
                $colModel=$_SESSION["ADP"][CCTRL."-JQX"]["colModel"];
            else{
                $colModel=$this->user_model->colmod();
                $_SESSION["ADP"][CCTRL."-JQX"]["colModel"]=$colModel;
            }
            header("Content-type:text/javascript");
            $this->smarty
                    ->assign("colModel", $colModel)
                    ->assign("display",$_SESSION["ADP"][CCTRL]["display"])
                    ->display("backend/base/grid_advanced");
        }
        function handleEvent(){
            
            header("Content-type:text/javascript");
            $time=time();
			$key=md5($_SESSION['auth']['user']->ause_key.$time);
			$this->smarty
                    ->assign(array(
						'time'=>$time,
						'key'=>$key
					))
                    ->display("backend/".CCTRL."/handleEvent");
        }
        function binding(){
            //$_POST=$_SESSION["tmp"];
            $result=$this->user_model->binding();
//            for ($i=0;$i<count($result['rows']);$i++) {
//                $result['rows'][$i]->_status = ($result['rows'][$i]->_status==="true");
//            }
            $data[] = array(
                'TotalRows' => $result['total_rows'],
                'Rows' => $result['rows']
            );
            $this->output->set_header('Content-type: application/json');
            $this->output->set_output(json_encode($data));
        }
		function setprivilege(){
			//die(json_encode($this->input->post(),true));
			$R["result"]=-998;$rOk=0;
			$R["message"]=ascii_to_entities("Access is denied - Request invalid.");
			$salt=$_SESSION['auth']['user']->ause_salt;
			$aupr_user=$this->input->post('user_id');
			$aupr_privilege=$this->input->post('privilege_id');
			$aupr_permission=$this->input->post('permission');
			$secretkey=$this->input->post('secretkey');
			$signature=$this->input->post('signature');
			if($this->privilege->aupr_permission!=='777'){
				$R["result"]=-997;
				$R["message"]=ascii_to_entities("Permission denied to access the function.");
				goto result;
			}
			//if( $signature!==md5("$aupr_privilege$secretkey$salt") ){
			//	$R["result"]=-991;
			//	$R["message"]=ascii_to_entities("Signature invalid.");
			//	goto result;
			//}
			$aParamsi=array(
				'aupr_user'=>$aupr_user,
				'aupr_privilege'=>$aupr_privilege,
				'aupr_permission'=>$aupr_permission,
				'aupr_insert'=>date('Y-m-d H:i:s'),
				'aupr_status'=>'true',
			);
			$aUpdate=array('aupr_permission','aupr_insert');
			//$aParamsi=objectToArray( json_decode('{"aupr_user":"10001","aupr_privilege":"10010","aupr_permission":"755","aupr_insert":"'.date('Y-m-d H:i:s').'","aupr_status":"true"}'));
			//die(json_encode($aParamsi,true));
			$rOk=$this->user_model->insert_onduplicate_update_aupr($aParamsi,$aUpdate);
			if($rOk>0){
				$R["result"]=1;
				$R["message"]=ascii_to_entities("Set permission success.");
			}else{
				$R["result"]=-1;
				$R["message"]=ascii_to_entities("Set permission fail.");
			}
			result:
			$this->output->set_header('Content-type: application/json');
            $this->output->set_output(json_encode($R));
		}
        function detail($id=""){
            //$id=$this->input->get('id');
			$user=$this->user_model->get($id);
			if($user===null)show_404();
            $privileges=$this->user_model->getprivileges($id);
            //echo $this->db->last_query();
			$time=time();
			$salt=$_SESSION['auth']['user']->ause_salt;
			$this->smarty
                    ->assign(array(
						'user'=>$user,
						'privileges'=>$privileges,
						'time'=>$time,
						'salt'=>$salt
					))
                    ->display("backend/".CCTRL."/01_detail");
        }
}
?>